package academic.concordia.bankofquebec.managedbean;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import org.springframework.context.annotation.Scope;

import academic.concordia.bankofquebec.bean.bankAccount.IBankAccountServices;
import academic.concordia.bankofquebec.bean.client.IClientBean;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.entity.Account;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Transaction;
//import academic.concordia.bankofquebec.entity.Transfer;

@ManagedBean(name="transfersManagedBean")
@Scope("session")
public class TransfersManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String toSelectedProduct;
	private String fromSelectedProduct;
	private Map<String, String> fromProducts;
	private Map<String, String> toProducts;
	private double amount;
	
	@ManagedProperty(value="#{SessionData}")
	private SessionData sessionData;
	
	@ManagedProperty(value="#{ClientBean}")
	IClientBean clientBean;
	
	@ManagedProperty(value="#{BankAccountBean}")
	IBankAccountServices bankAccountBean;
	
	@PostConstruct
    public void init() {
		loadFromAccounts();
		toProducts = new LinkedHashMap<String, String>(fromProducts);
    }
	
	public String formatAccountNumber(String accountNumber) {
		String protectedNumber = accountNumber.substring(0, accountNumber.length() - 4).replaceAll("[0-9]*", "*");
		return protectedNumber + accountNumber.substring(accountNumber.length() - 4);
	}
	
	public void updateToProducts(AjaxBehaviorEvent event) {
		BankAccount fromAccount = bankAccountBean.getAccountById(fromSelectedProduct);
		toProducts = new LinkedHashMap<String, String>();
		List<BankAccount> products = bankAccountBean.getOtherTransferAccounts(sessionData.getClient(), fromAccount);
		
		for(BankAccount account : products) {
			toProducts.put(account.getAccountNumber(), (account.getType().toString() + formatAccountNumber(account.getAccountNumber())));
		}
	}
	
	public void loadFromAccounts(){
		fromProducts = new LinkedHashMap<String, String>();
		List<BankAccount> products = bankAccountBean.getTransferAccounts(sessionData.getClient());
		
		for(BankAccount account : products) {
			fromProducts.put(account.getAccountNumber(), (account.getType().toString() + formatAccountNumber(account.getAccountNumber())));
		}
	}
	
	public String executeTransfer() {
		System.out.println("Transaction ");
		System.out.println("FromAccount " + fromSelectedProduct);
		System.out.println("Transaction " + toSelectedProduct);		
		BankAccount fromAccount = bankAccountBean.getAccountById(fromSelectedProduct);
		BankAccount toAccount = bankAccountBean.getAccountById(toSelectedProduct);
		Transaction transaction = bankAccountBean.transfer(fromAccount, toAccount, amount);
		System.out.println("Transaction response " + transaction.getStatus().name() + transaction.getMessage() + " " + transaction.isApproved());
		if (!transaction.isApproved()){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			FacesMessage facesMessage = new FacesMessage(transaction.getMessage());
			facesContext.addMessage(null, facesMessage);
		}
			
		return "";
	}

	public Map<String, String> getFromProducts() {
		return fromProducts;
	}

	public void setFromProducts(Map<String, String> fromProducts) {
		this.fromProducts = fromProducts;
	}

	public Map<String, String> getToProducts() {
		return toProducts;
	}

	public void setToProducts(Map<String, String> toProducts) {
		this.toProducts = toProducts;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public SessionData getSessionData() {
		return sessionData;
	}

	public void setSessionData(SessionData sessionData) {
		this.sessionData = sessionData;
	}

	public IClientBean getClientBean() {
		return clientBean;
	}

	public void setClientBean(IClientBean clientBean) {
		this.clientBean = clientBean;
	}

	public IBankAccountServices getBankAccountBean() {
		return bankAccountBean;
	}

	public void setBankAccountBean(IBankAccountServices bankAccountBean) {
		this.bankAccountBean = bankAccountBean;
	}

	public String getToSelectedProduct() {
		return toSelectedProduct;
	}

	public void setToSelectedProduct(String toSelectedProduct) {
		this.toSelectedProduct = toSelectedProduct;
	}

	public String getFromSelectedProduct() {
		return this.fromSelectedProduct;
	}

	public void setFromSelectedProduct(String fromSelectedProduct) {
		this.fromSelectedProduct = fromSelectedProduct;
	}
}
